import { G_S } from "../../config/g_s";
import PopupManager from "../../core/PopupManager";
import PopupBase from "./PopupBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class RealNameConfirm extends PopupBase<RealNameConfirmOptions> {

    @property(cc.EditBox) edx_name: cc.EditBox = null;
    @property(cc.EditBox) edx_cardNum: cc.EditBox = null;

    @property(cc.Node)
    private confirmBtn: cc.Node = null;

    
    protected onLoad() {
        this.registerEvent();
    }

    protected onDestroy() {
        this.unregisterEvent();
    }

    private registerEvent() {
        this.confirmBtn.on(cc.Node.EventType.TOUCH_END, this.onConfirmBtnClick, this);
    }

    private unregisterEvent() {
        this.confirmBtn.targetOff(this);
    }

    protected init(options: RealNameConfirmOptions) {

    }

    protected updateDisplay(options: RealNameConfirmOptions): void {
        
    }

    protected onConfirmBtnClick() {
        // this.options.confirmCallback && this.options.confirmCallback();
        // this.hide();
        let name = this.edx_name.string;
        let code = this.edx_cardNum.string;
        let regName = /^[\u4E00-\u9FA5]{2,4}$/;
        if (name == "" && code == "") {
            PopupManager.show('prefabs/PopBox', {content: G_S.REGISTER_JS_1044},{immediately:true});
            return;
        }
        if (name == "") {
            PopupManager.show('prefabs/PopBox', {content: G_S.TIP_REALNAMECONFIRM_JS_1828},{immediately:true});
            return;
        }
        if (!regName.test(name)) {
            PopupManager.show('prefabs/PopBox', {content: G_S.TIP_REALNAMECONFIRM_JS_1829},{immediately:true});
            return;
        }
        if (code == "") {
            PopupManager.show('prefabs/PopBox', {content: G_S.TIP_REALNAMECONFIRM_JS_1830},{immediately:true});
            return;
        }
        if (!this.IdentityCodeValid(code)) {
            return;
        }
    }

    protected IdentityCodeValid(code:string) {
        var city = {
            11: G_S.TIP_REALNAMECONFIRM_JS_1788,
            12: G_S.TIP_REALNAMECONFIRM_JS_1789,
            13: G_S.TIP_REALNAMECONFIRM_JS_1790,
            14: G_S.TIP_REALNAMECONFIRM_JS_1791,
            15: G_S.TIP_REALNAMECONFIRM_JS_1792,
            21: G_S.TIP_REALNAMECONFIRM_JS_1793,
            22: G_S.TIP_REALNAMECONFIRM_JS_1794,
            23: G_S.TIP_REALNAMECONFIRM_JS_1795,
            31: G_S.TIP_REALNAMECONFIRM_JS_1796,
            32: G_S.TIP_REALNAMECONFIRM_JS_1797,
            33: G_S.TIP_REALNAMECONFIRM_JS_1798,
            34: G_S.TIP_REALNAMECONFIRM_JS_1799,
            35: G_S.TIP_REALNAMECONFIRM_JS_1800,
            36: G_S.TIP_REALNAMECONFIRM_JS_1801,
            37: G_S.TIP_REALNAMECONFIRM_JS_1802,
            41: G_S.TIP_REALNAMECONFIRM_JS_1803,
            42: G_S.TIP_REALNAMECONFIRM_JS_1804,
            43: G_S.TIP_REALNAMECONFIRM_JS_1805,
            44: G_S.TIP_REALNAMECONFIRM_JS_1806,
            45: G_S.TIP_REALNAMECONFIRM_JS_1807,
            46: G_S.TIP_REALNAMECONFIRM_JS_1808,
            50: G_S.TIP_REALNAMECONFIRM_JS_1809,
            51: G_S.TIP_REALNAMECONFIRM_JS_1810,
            52: G_S.TIP_REALNAMECONFIRM_JS_1811,
            53: G_S.TIP_REALNAMECONFIRM_JS_1812,
            54: G_S.TIP_REALNAMECONFIRM_JS_1813,
            61: G_S.TIP_REALNAMECONFIRM_JS_1814,
            62: G_S.TIP_REALNAMECONFIRM_JS_1815,
            63: G_S.TIP_REALNAMECONFIRM_JS_1816,
            64: G_S.TIP_REALNAMECONFIRM_JS_1817,
            65: G_S.TIP_REALNAMECONFIRM_JS_1818,
            71: G_S.TIP_REALNAMECONFIRM_JS_1819,
            81: G_S.TIP_REALNAMECONFIRM_JS_1820,
            82: G_S.TIP_REALNAMECONFIRM_JS_1821,
            91: G_S.TIP_REALNAMECONFIRM_JS_1822
        };
        var tipMsg = "";
        var pass = true;
        var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
        if (!code || !regIdCard.test(code)) {
            tipMsg = G_S.TIP_REALNAMECONFIRM_JS_1823;
            pass = false;
        } else if (!city[code.substr(0, 2)]) {
            tipMsg = G_S.TIP_REALNAMECONFIRM_JS_1825;
            pass = false;
        } else {
            //18位身份证需要验证最后一位校验位
            if (code.length == 18) {
                var codes = code.split('');
                //∑(ai×Wi)(mod 11)
                //加权因子
                var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                //校验位
                var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
                var sum = 0;
                var ai = 0;
                var wi = 0;
                for (var i = 0; i < 17; i++) {
                    ai = parseInt(codes[i]); 
                    wi = factor[i];
                    sum += ai * wi;
                }
                if (parity[sum % 11] != codes[17].toUpperCase()) {
                    tipMsg = G_S.TIP_REALNAMECONFIRM_JS_1827;
                    pass = false;
                }
            }
        }
        if (!pass) {
            PopupManager.show('prefabs/PopBox', {content: tipMsg},{immediately:true});
        }
        return pass;
    }

    //发送实名认证
    protected verifued(cardName:string, cardNumber:string, cbSuccess:Function) {
        // var params = {};
        // params["t"] = mAccount.token;
        // params["n"] = cardName;
        // params["id"] = cardNumber;
        // DC.httpPost2(DC.getHttpUrl("/user/Verified", DC.LU), params,
        //     function (result) {
        //         var age = result["age"];
        //         var ret = result["ret"];
        //         var status = result["status"];
        //         if(status == 0){
        //             mAccount.setUserAge(age);
        //             HUD.showMessage(G_S.REGISTER_JS_1037);
        //             if (cbSuccess) {
        //                 cbSuccess();
        //             }
        //         } else {
        //             HUD.showMessage(G_S.REGISTER_JS_1043);
        //         }
        //     }
        // );
    }

}

/** 
 * 确认弹窗选项 
 * @param confirmCallback 确认回调
 */
 export interface RealNameConfirmOptions {
    confirmCallback: Function;
}
